import 'package:cbec_app/components/base/base_chekbox.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/common/recommend.dart';
import 'package:cbec_app/components/goods/bags_goods_item.dart';
import 'package:cbec_app/components/goods/goods_info_item.dart';
import 'package:cbec_app/pages/bags/controller/bags_ctr.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../utils/AppData.dart';

///购物袋页
class BagsPage extends GetView<BagsController> {
  BagsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BagsController>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Tools.fromHex('#FFFFFF'),
          appBar: AppBar(
            title: const Text(
              '购物车',
              style: TextStyle(fontSize: 17, color: Colors.black, fontWeight: FontWeight.w500),
            ), // 设置页面标题
            centerTitle: true, // 标题居中显示
            backgroundColor: Tools.fromHex('#FFFFFF'),
            elevation: 0, // 去掉阴影
          ), // ,
          body: Column(
            children: [
              Container(
                height: 30,
                child: controller.showGoodNum > 0 ? _goodsNumMgr() : const SizedBox(),
              ),
              Expanded(
                child: Container(
                  color: Tools.fromHex('#F4F4F4'),
                  padding: const EdgeInsets.all(8),
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        // 商品列表
                        controller.goodsGroupList.isNotEmpty
                            ? Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(8),
                                ),
                                child: ListView.builder(
                                  // padding: const EdgeInsets.symmetric(vertical: 20),
                                  shrinkWrap: true,
                                  physics: const NeverScrollableScrollPhysics(),
                                  itemCount: controller.goodsGroupList.length,
                                  itemBuilder: (context, index) {
                                    return BagsGoodsItem(
                                      groupInfo: controller.goodsGroupList[index],
                                      btnType: controller.isBatchMgr ? RightBtnType.delete : RightBtnType.addMinus,
                                      groupIndex: index,
                                      checkboxCallback: (groupIndex, index, isCheck) {
                                        // controller.toggleSelectSingleGoods(groupIndex,index,isCheck);
                                      },
                                      onDelete: (id, groupIndex, index) {
                                        controller.deleteCarGoods(id, groupIndex, index);
                                      },
                                      onCollect: (id) {
                                        controller.collectGoods(id);
                                      },
                                    );
                                  },
                                ),
                              )
                            : const NotFound(
                                txt: '购物车没有商品，请添加商品',
                              ),
                        Recommend(location: "Cart"),
                      ],
                    ),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
                child: controller.isBatchMgr ? _managerCar(context) : _goodsSettlement(context),
              ),
            ],
          ),
        );
      },
    );
  }

  ///商品数量及管理
  Widget _goodsNumMgr() {
    double padding = ThemeConfig.bodyPadding / 2;
    return Container(
      color: Tools.fromHex('#F4F4F4'),
      padding: EdgeInsets.only(
        top: 8,
        left: padding.w,
        right: padding.w,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            '购物车(${controller.showGoodNum})',
            style: setTextStyle(15, FontWeight.w700, ThemeConfig.boldTextColor),
          ),
          controller.showGoodNum > 0
              ? BaseInteractive(
                  childWidget: Text(
                    controller.isBatchMgr ? '完成' : '管理',
                    style: setTextStyle(13, FontWeight.w400, ThemeConfig.boldTextColor),
                  ),
                  onClick: () {
                    // Tools.showToast('113132142141254');
                    controller.clickManager();
                  },
                )
              : const SizedBox(),
        ],
      ),
    );
  }

  ///商品结算
  Widget _goodsSettlement(BuildContext context) {
    return Container(
        key: GlobalKey(debugLabel: 'Positioned'),
        child: controller.goodsGroupList.isNotEmpty
            ? Container(
                width: 375.w,
                height: 48.h,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Padding(
                          padding: EdgeInsets.only(left: 10.w),
                          child: BaseCheckBox(
                            value: controller.isSelectAll,
                            onChange: (selected) {
                              controller.toggleAllSelect(selected);
                            },
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 10.w),
                          child: Text(
                            '全选',
                            style: setTextStyle(12, FontWeight.w500, ThemeConfig.mainTextColor),
                          ),
                        ),
                        Container(
                            margin: const EdgeInsets.only(left: 10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Row(
                                  children: [
                                    Text(
                                      '已选${controller.totalNum}件,合计:',
                                      style: setTextStyle(12, FontWeight.w500, ThemeConfig.mainTextColor),
                                    ),
                                    Text(
                                      '￥${controller.pricePay}',
                                      style: setTextStyle(10, FontWeight.w500, ThemeConfig.themeColor),
                                    ),
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text(
                                      '优惠减￥${controller.carInfo['priceDiscount'] ?? 0}',
                                      style: setTextStyle(10, FontWeight.w400, ThemeConfig.sectionTextColor),
                                    ),
                                    BaseInteractive(
                                      childWidget: Container(
                                          margin: const EdgeInsets.only(left: 5),
                                          child: Text(
                                            '优惠明细',
                                            style: setTextStyle(10, FontWeight.w700, ThemeConfig.mainTextColor),
                                          )),
                                      onClick: () {
                                        controller.toggleDetail(context);
                                      },
                                    )
                                  ],
                                )
                              ],
                            ))
                      ],
                    ),
                    BaseInteractive(
                      childWidget: Builder(
                        builder: (context1) {
                          return Container(
                            width: 88.w,
                            height: 40,
                            alignment: Alignment.center,
                            margin: const EdgeInsets.only(right: 8),
                            decoration: const BoxDecoration(color: ThemeConfig.themeColor, borderRadius: BorderRadius.all(Radius.circular(50))),
                            child: Text(
                              '去结算',
                              style: setTextStyle(14, FontWeight.w500, Colors.white),
                            ),
                          );
                        },
                      ),
                      onClick: () {
                        controller.goodsSettlement();
                      },
                    )
                  ],
                ),
              )
            : emptyWidget());
  }

  //购物车管理
  Widget _managerCar(BuildContext context) {
    return Container(
        key: GlobalKey(debugLabel: 'Positioned'),
        child: Container(
          width: 375.w,
          height: 48.h,
          color: Colors.white,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Padding(
                    padding: EdgeInsets.only(left: 10.w),
                    child: BaseCheckBox(
                      value: controller.isSelectAll,
                      onChange: (selected) {
                        controller.toggleAllSelect(selected);
                      },
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 10.w),
                    child: Text(
                      '全选',
                      style: setTextStyle(12, FontWeight.w500, ThemeConfig.mainTextColor),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  BaseInteractive(
                    childWidget: Container(
                      width: 90.w,
                      height: 40.h,
                      alignment: Alignment.center,
                      decoration: const BoxDecoration(color: Color(0xffF7CA36), borderRadius: BorderRadius.all(Radius.circular(50))),
                      child: Text(
                        '移入收藏',
                        style: setTextStyle(12, FontWeight.w500, Colors.white),
                      ),
                    ),
                    onClick: () {
                      controller.multiCollectGoods();
                    },
                  ),
                  BaseInteractive(
                    childWidget: Container(
                      width: 90.w,
                      height: 40.h,
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: 8, right: 18.w),
                      decoration: const BoxDecoration(color: ThemeConfig.themeColor, borderRadius: BorderRadius.all(Radius.circular(50))),
                      child: Text(
                        '删除',
                        style: setTextStyle(12, FontWeight.w500, Colors.white),
                      ),
                    ),
                    onClick: () {
                      controller.multiDeleteGoods();
                    },
                  )
                ],
              ),
            ],
          ),
        ));
  }
}
